package com.itestor.iptv.modules.alert.actions;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.itestor.iptv.modules.alert.bean.RobotLogQuery;
import com.itestor.iptv.modules.alert.service.RobotLogService;
import com.itestor.iptv.modules.alert.vo.RobotLogBean;
import com.itestor.iptv.modules.common.actions.CommonExcelExportAction;

public class RobotLogHistoryExportExcelAction extends Action{
	private static SimpleDateFormat  sdf =new SimpleDateFormat("yyyy-MM-dd HH:mm",Locale.US);

    private RobotLogService robotLogService;
    
	public void setRobotLogService(RobotLogService robotLogService) {
		this.robotLogService = robotLogService;
	}

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String robotName = (String) request.getParameter("robotName");
        String confirmStatus = (String) request.getParameter("confirmStatus");
        String startTime = (String) request.getParameter("startTime");
        String endTime = (String) request.getParameter("endTime");
        
        RobotLogQuery robotLogQuery = new RobotLogQuery();
        if (!StringUtils.isEmpty(robotName)) {
            robotLogQuery.setRobotName(robotName);
            request.setAttribute("robotName", robotName);
        }

        if (!StringUtils.isEmpty(confirmStatus)) {
            robotLogQuery.setConfirmStatus(Integer.parseInt(confirmStatus));
            request.setAttribute("confirmStatus", confirmStatus);
        }

        if (!StringUtils.isEmpty(startTime)) {
            Date start=sdf.parse(startTime);
            robotLogQuery.setStartLogTime(start.getTime()/1000);
            request.setAttribute("startTime", startTime);
        }
        
        if (!StringUtils.isEmpty(endTime)) {
            Date end=sdf.parse(endTime);
            robotLogQuery.setEndLogTime(end.getTime()/1000);            
            request.setAttribute("endTime", endTime);
        }        
     
        robotLogService.getHisRobotLogList(robotLogQuery);
        
		Collection<RobotLogBean> robotLogCurrentList = robotLogQuery.getQueryResults();
		Collection<Object[]> returnList = new ArrayList<Object[]>();
		for(RobotLogBean temp : robotLogCurrentList){
			Object[] o = new Object[2];
			o[0] = temp;
			o[1] = temp.getRobotlog().getConfirm()==1?"已确认":(temp.getRobotlog().getConfirm()==0?"未确认":"");
			returnList.add(o);
		}
		String templateFileName = Thread.currentThread()
				.getContextClassLoader()
				.getResource("robotLogCurrent-template.xls").getFile();
		Map<String, Object> dataMap = new HashMap<String, Object>();
		dataMap.put("robotLogCurrentList", returnList);
		request.setAttribute(CommonExcelExportAction.EXCEL_TEMPLATE_NAME,
				templateFileName);
		request.setAttribute(CommonExcelExportAction.EXCEL_FILE_NAME,
				"历史告警.xls");
		request.setAttribute(CommonExcelExportAction.EXCEL_DATAMAP_NAME,
				dataMap);
		request.getRequestDispatcher("CommonExcelExportAction.do").forward(
				request, response);
		return null;
	}
}
